using System;
using System.Drawing;

using XPTable.Renderers;
using FlexminAPI;

namespace FlexminAdmin.Search
{
	class ComputerCellRenderer : TextCellRenderer
	{
		private SolidBrush foreBrush;

		public ComputerCellRenderer()
			: base()
		{
		}

		protected override void OnPaintBackground(XPTable.Events.PaintCellEventArgs e)
		{
			base.OnPaintBackground(e);
			if (!(e.Cell.Data is IHost))
				return;

			double mid;
			switch ((e.Cell.Data as IHost).State)
			{
				case ComputerState.Off:
					mid = ((int) this.BackColor.R 
						+ (int) this.BackColor.G + 
						(int) this.BackColor.B)
					/ 3;

					mid /= 1.2;

					BackColor = Color.FromArgb((int) mid, (int) mid, (int) mid);
					break;
					
				case ComputerState.On:
					mid = ((int)this.BackColor.R
						+ (int)this.BackColor.G +
						(int)this.BackColor.B)
					/ 3;

					mid /= 1.2;

					BackColor = Color.FromArgb(
						(int)(BackColor.R / 1.2),
						(byte)(BackColor.G / 1.2),
						(int)(BackColor.B + + ((255 - BackColor.B) / 1.2))
						);
					break;

				case ComputerState.Ready:
				case ComputerState.NotAutentificated:
					BackColor = Color.FromArgb(
						(int) ( BackColor.R / 1.2 ),
						(byte) ( BackColor.G + ((255 - BackColor.G) / 1.2) ),
						(int) ( BackColor.B / 1.2 )
						);
					break;
			}

			e.Graphics.FillRectangle(this.BackBrush, e.CellRect);
		}

		protected override void OnPaint(XPTable.Events.PaintCellEventArgs e)
		{
			if (!(e.Cell.Data is IHost))
				return;

			if (foreBrush == null
				|| foreBrush.Color != e.Table.ForeColor)
				foreBrush = new SolidBrush(e.Table.ForeColor);

			e.Graphics.DrawString((e.Cell.Data as IHost).HostAddress, e.Table.Font, 
				foreBrush, e.CellRect);
		}
	}
}
